package auth

import (
	"context"

	"mall/common/globalkey"
	"mall/common/jwt"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/user/rpc/userclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserAuthByUserIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取UserAddress列表
func NewGetUserAuthByUserIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserAuthByUserIdLogic {
	return &GetUserAuthByUserIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserAuthByUserIdLogic) GetUserAuthByUserId(
	req *types.GetUserAuthByUserIdReq,
	headers *[]*types.Header,
) (resp *types.UserAuthInfo, err error) {
	logx.Infof("GetUserAuthByUserId : %+v ", req)
	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}

	res, err := l.svcCtx.UserRpc.GetUserAuthByUserId(l.ctx, &userclient.GetUserAuthByUserIdReq{
		PlatformId:  pointy.GetPointer(globalkey.ThirdPartyPlatformWechatMini),
		UserId:      pointy.GetPointer(userId),
		WithRank:    req.WithRank,
		WithRole:    req.WithRole,
		WithPoint:   req.WithPoint,
		WithBalance: req.WithBalance,
	})
	if err != nil {
		return nil, err
	}

	resp = &types.UserAuthInfo{
		UserId:           res.UserId,
		PlatformId:       res.PlatformId,
		Openid:           res.Openid,
		Unionid:          res.Unionid,
		SessionKey:       res.SessionKey,
		Nickname:         res.Nickname,
		Avatar:           res.Avatar,
		Gender:           res.Gender,
		Country:          res.Country,
		Province:         res.Province,
		City:             res.City,
		Language:         res.Language,
		Status:           res.Status,
		CreatedAt:        res.CreatedAt,
		UpdatedAt:        res.UpdatedAt,
		Mobile:           res.Mobile,
		Birthday:         res.Birthday,
		Source:           res.Source,
		BirthdayHidden:   res.BirthdayHidden,
		Authorized:       res.Authorized,
		CreatedIP:        res.CreatedIP,
		RankId:           res.RankId,
		RoleId:           res.RoleId,
		AvailablePoint:   res.AvailablePoint,
		HistoryPoint:     res.HistoryPoint,
		AvailableBalance: res.AvailableBalance,
		UserUUID:         res.UserUUID,
	}

	*headers = []*types.Header{
		{
			Key:   "x-class",
			Value: "com.djx.appview.UserAddressPage",
		},
	}

	return resp, nil
}
